package cn.ibobo.lease.common.util;

import cn.ibobo.lease.common.enums.ResultEnum;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * @Description 阿里云短信发送
 * @Author lililiang
 * @Date 2019/2/21 19:04
 * @Param
 * @Return
 */
public class SMSUtil {

    /**
     * @Description 发送短信
     * @Author lililiang
     * @Date 2019/2/21 19:18
     * @Param [mobile, templateCode]
     * @Return java.lang.Integer
     */
    public static Integer sendSms(String mobile,String templateCode, String SMSCode){

        try{
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", Constant.ACCESS_KEY_ID, Constant.ACCESS_KEY_SECRET);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", Constant.PRODUCT, Constant.DOMAIN);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            //必填:待发送手机号 支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,
            //验证码类型的短信推荐使用单条调用的方式；发送国际/港澳台消息时，接收号码格式为00+国际区号+号码，如“0085200000000”
            request.setPhoneNumbers(mobile);
            request.setSignName(Constant.SIGN_NAME);
            request.setTemplateCode(templateCode);//SMS_137390044
            request.setTemplateParam("{\"code\":\""+ SMSCode +"\"}");//发送内容中的参数,JSON形式
            //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
            request.setOutId("yourOutId");
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if (null == sendSmsResponse) {
                return ResultEnum.FAIL.getCode();
            }
            System.out.println("===============================");
            System.out.println("短信接口回调码resCode = " + sendSmsResponse.getCode() + ", 提示信息message = "  + sendSmsResponse.getMessage());
            System.out.println("===============================");
            if("OK".equals(sendSmsResponse.getCode())){
                return ResultEnum.SUCCESS.getCode();
            }else{
                return ResultEnum.FAIL.getCode();
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultEnum.FAIL.getCode();
        }
    }

    /**
     * @Description 生成6位随机数字
     * @Author lililiang
     * @Date 2019/2/21 20:10
     * @Param [min, max]  ("%04d", 99)
     * @Return java.lang.Integer
     */
    public static String getRandNum() {
        int randNum = (int)(Math.random() * 1000000);
        return String.format("%06d", randNum);
    }
    public static void main(String[] args) {
        sendSms("13931854366","SMS_162522056","123695");
//        for(int i=0 ; i< 10 ; i++){
//
//            System.out.println(getRandNum());
//        }
    }

}